using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Hermes.Framework.FileSystem;

namespace Hermes.Framework.FileSystem.Local
{
    public class LocalFileSystem : INonTransactableFileSystem
    {
        public void Connect(ConnectionInfo connectionInfo)
        {

        }

        protected static string PreprocessFileName(string fileName)
        {
            fileName = FileSystemUtils.GetFileName(fileName);
            fileName = FileSystemUtils.GetRelativePath(fileName);
            return fileName;
        }

        public void CreateFile(string fileName)
        {
            fileName = PreprocessFileName(fileName);
            FileStream file = File.Create(fileName);
            file.Close();
        }

        public void Copy(string from, string to)
        {
            from = PreprocessFileName(from);
            to = PreprocessFileName(to);
            Console.WriteLine("Copy {0} to {1}", from, to);
            File.Copy(from, to);
        }

        public void Rename(string from, string to)
        {
            from = PreprocessFileName(from);
            to = PreprocessFileName(to);
            File.Move(from, to);
        }

        public void Delete(string fileName)
        {
            fileName = PreprocessFileName(fileName);
            File.Delete(fileName);
        }

        public bool Exists(string fileName)
        {
            fileName = PreprocessFileName(fileName);
            return File.Exists(fileName);
        }

        public bool IsDirectory(string directoryName)
        {
            directoryName = PreprocessFileName(directoryName);
            return Directory.Exists(directoryName);
        }

        public void CreateDirectory(string directoryName)
        {
            directoryName = PreprocessFileName(directoryName);
            Directory.CreateDirectory(directoryName);
        }

        public void RemoveDirectory(string directoryName)
        {
            directoryName = PreprocessFileName(directoryName);
            Directory.Delete(directoryName);
        }

        public int GetItemCount(string directoryName)
        {
            directoryName = PreprocessFileName(directoryName);
            if (Directory.Exists(directoryName))
            {
                return Directory.GetDirectories(directoryName).Length + Directory.GetFiles(directoryName).Length;
            }
            return 0;
        }

        public string GetItem(string directoryName, int index)
        {
            directoryName = PreprocessFileName(directoryName);
            if (index < Directory.GetDirectories(directoryName).Length)
            {
                string[] directories = Directory.GetDirectories(directoryName);
                return directories[index];
            }
            index = index - Directory.GetDirectories(directoryName).Length;
            string[] files = Directory.GetFiles(directoryName);
            return Path.GetFileName(files[index]);
        }

        public void RenameDirectory(string from, string to)
        {
            string f = PreprocessFileName(from);
            string t = PreprocessFileName(to);
            Directory.Move(f, t);
        }

        public void Dispose()
        {
            // does nothing...
        }
    }
}
